//
//  ZZCustomRevertAnaylsisVC.m
//  新项目
//
//  Created by liGordon on 17/3/10.
//  Copyright © 2017年 刘耀宗. All rights reserved.
//

#import "ZZCustomRevertAnaylsisVC.h"
#import "ZZCustomRevertAnalysisCell.h"
#import "ZZCustomRevertAnalysisDetailVC.h"
#import "ZZCustomViewModel.h"
#import "ZZCustomConvertModel.h"
#import "ZZCustomConvertContentModel.h"

static NSString *identifier = @"CustomRevertAnalysisCell";
@interface ZZCustomRevertAnaylsisVC ()<UITableViewDelegate,UITableViewDataSource,ZZCustomRevertAnalysisCellDelegate>
@property (weak, nonatomic) IBOutlet UIButton *searchBtn;
@property (nonatomic, strong) CcNoDataView *noDataView;
@property (weak, nonatomic) IBOutlet UITableView *menuTable;
@property (nonatomic, assign) NSInteger total;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, assign) NSInteger num;
@property (nonatomic,strong)NSString *recommendStr;
@property (weak, nonatomic) IBOutlet UITextField *inputText;
@property (weak, nonatomic) IBOutlet UIView *inputView;
@property (nonatomic,strong)NSMutableArray *dataArr;
@end

@implementation ZZCustomRevertAnaylsisVC

-(NSMutableArray *)dataArr
{
 
        if (!_dataArr) {
            _dataArr = [NSMutableArray array];
        }
        return _dataArr;

}
- (IBAction)clickForSelect:(id)sender {
    self.recommendStr = self.inputText.text;
    [self.menuTable.mj_header beginRefreshing];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTableView];
    self.menuTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.title = @"客户转化分析";
        self.recommendStr = @"";
    MJRefreshNormalHeader *header=[MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.index=1;
        [self initDataWithIndex:[NSString stringWithFormat:@"%ld",self.index]];
              [self.menuTable.mj_footer  resetNoMoreData];
    }];
    self.menuTable.mj_footer=[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.total>upNum*self.index) {
            self.index++;
       [self initDataWithIndex:[NSString stringWithFormat:@"%ld",self.index]];

        }
        else
        {
            [self.menuTable.mj_footer endRefreshingWithNoMoreData];
            
        }
        
    }];
    
    
    self.menuTable.mj_header=header;
    [self.menuTable.mj_header beginRefreshing];
    
    
       self.inputView.layer.masksToBounds = YES;
    self.inputView.layer.cornerRadius = 8;
    self.searchBtn.layer.masksToBounds = YES;
    self.searchBtn.layer.cornerRadius = 8;
}

-(CcNoDataView *)noDataView
{
    if (_noDataView == nil) {
        _noDataView = [[CcNoDataView alloc] init];
        _noDataView.backgroundColor=[UIColor whiteColor];
    }
    return _noDataView;
    
}

- (void)setupTableView
{
    _menuTable.delegate = self;
    _menuTable.dataSource = self;
    _menuTable.rowHeight = 80;
    [_menuTable registerNib:[UINib nibWithNibName:@"ZZCustomRevertAnalysisCell" bundle:nil] forCellReuseIdentifier:identifier];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZZCustomRevertAnalysisCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = _dataArr[indexPath.row];
    cell.delegate = self;
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    
    ZZCustomRevertAnalysisDetailVC *vc = [[ZZCustomRevertAnalysisDetailVC alloc]init];
    vc.model = self.dataArr[indexPath.row];
    PushVC(vc)
    
}


#pragma mark -数据请求
-(void)initDataWithIndex:(NSString *)index
{
 
    NSUserDefaults *loginDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary   *loginDict = [loginDefaults objectForKey:@"loginInfo"];
    [[ZZCustomViewModel shareViewModel]getCustomConvertAnalysisWithCompanyID:loginDict[@"CompanyID"] andPageIndex:index andPageSize:@(upNum) andRecommendName:self.recommendStr andSuccess:^(NSDictionary *result) {
        
        if (result) {
            
            [ZZCustomConvertModel mj_setupObjectClassInArray:^NSDictionary *{
                return @{
                         @"rows" : @"ZZCustomConvertContentModel",
                         };
            }];
            ZZCustomConvertModel *fg = [ZZCustomConvertModel mj_objectWithKeyValues:result[@"resultJson"]];
        
            self.total = [fg.total integerValue];
            
            if (!ceilf([fg.total floatValue] / 10.00)) {
                NSInteger x = 0;
                self.num = x;
            }else{
                NSInteger x = ceilf([fg.total floatValue] / 10.00);
                self.num = x;
            }
            
            NSMutableArray *mutableArr=[NSMutableArray array];
            for (NSDictionary *dic in fg.rows) {
                [mutableArr addObject:dic];
            }
            
            
            if (self.index!=1) {
                if (self.index > self.num) {
                    [self.menuTable.mj_footer endRefreshingWithNoMoreData];
                    return ;
                }else{
                         [self.dataArr addObjectsFromArray: mutableArr];
                }
            [self.menuTable.mj_footer endRefreshing];
            }
            
            else
            {
                self.dataArr=mutableArr;
                [self.menuTable.mj_header endRefreshing];
                
            }


            
            
            
            
            
            
            
            [self.noDataView dataIsEmpty:self.dataArr withView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 130, newwidth, newheight-130) andBtnHiddne:YES];
            self.noDataView.label.text=@"暂无数据";
            
            [self.menuTable reloadData];
            

        }


               } andFailer:^{
                   [self.menuTable.mj_header endRefreshing];
                   [self.noDataView noNetwithView:self.menuTable andNoDataView:self.noDataView andRect:CGRectMake(0, 0, newwidth, newheight) andBtnHiddne:NO];
                   //点击重新加载的block
                   WEAKSELF
                   self.noDataView.myBlock=^{
                       [weakSelf.menuTable.mj_header beginRefreshing];
                   };
                   
               }];

}

#pragma mark - 跳转详情

-(void)clickDetail:(ZZCustomConvertContentModel *)model
{
    ZZCustomRevertAnalysisDetailVC *vc = [[ZZCustomRevertAnalysisDetailVC alloc]init];
    vc.model = model;
    PushVC(vc)
}
@end
